import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../../Pojo/Search_Itme_Pojo.dart';
//搜索
class Search extends StatefulWidget {
  const Search({Key? key}) : super(key: key);

  @override
  State<Search> createState() => _SearchState();
}

class _SearchState extends State<Search> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: Container(
          child:GestureDetector(
            onTap: (){
              Navigator.pop(context);
            },
            child: Icon(Icons.arrow_circle_left,color: Colors.blue,size: 40),
          ),
        ),
        backgroundColor: Colors.white,
        title: Text("搜索",style: TextStyle(
          color: Colors.black,
          fontSize: 26
        ),),
        elevation: 0.0,
      ),
      body: SearchIndex(),
    );
  }
}

class SearchIndex extends StatefulWidget {
  const SearchIndex({Key? key}) : super(key: key);

  @override
  State<SearchIndex> createState() => _SearchIndexState();
}

class _SearchIndexState extends State<SearchIndex> {

  String _SearchValue="";

  var _timer;

  //数据列表
  late List<SearchItmePojo> _SearchItmeList ;

  //组件渲染列表
  List<Widget> _SearchItmeWidgetList =[];



  void SetSearchValue(String value){
    setState(() {
      _SearchValue=value;
      _SearchItmeWidgetList=[];
    });
  }

  void InterGetSearchList(String value){
  //  发送请求添加数据;

    var axios=[
      SearchItmePojo("新闻1", "1"),
      SearchItmePojo("新闻2", "2"),
      SearchItmePojo("新闻3", "3"),
      SearchItmePojo("新闻4", "4"),
    ];

    setState(() {
      _SearchItmeList=axios;
    });
    NewItmeWidgetList(axios);
  }

  //渲染数据推荐列表
  void NewItmeWidgetList(List<SearchItmePojo> SearchItmeList){
      _SearchItmeWidgetList=[];
    for(int i=0;i<SearchItmeList.length;i++){
      setState(() {
        _SearchItmeWidgetList.add(
            SearchItme(SearchValue: SearchItmeList[i].ItmeName, SetSearch: SetSearchValue)
        );
      });
      if(_SearchItmeWidgetList==[]){
        _SearchItmeWidgetList=[];
      }
    }
  }


  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      scrollDirection: Axis.vertical,
      child: Container(
        margin: EdgeInsets.all(8),
        alignment: Alignment.center,
        child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                Container(
                  height: 40,
                  width: 260,
                  decoration: BoxDecoration(
                      border: Border.all(
                        color: Colors.black12,
                        width: 1,
                      )
                  ),
                  child: TextField(
                    controller: TextEditingController.fromValue(
                      TextEditingValue(
                        text: _SearchValue,
                        selection:  TextSelection.fromPosition(TextPosition(
                            affinity: TextAffinity.downstream,
                            offset: _SearchValue.length)
                        )
                      )
                    ),
                    cursorHeight: 40,
                    style: TextStyle(
                          fontSize: 30,
                      ),
                    decoration: InputDecoration(
                        isDense: true,
                        border:InputBorder.none,
                        hintText: " 输入搜索内容",
                        hintStyle: TextStyle(
                          height: 0,
                          fontSize: 20,
                        )
                      ),
                    onChanged: (value){
                      _timer?.cancel();
                      this._timer= new Timer(Duration(milliseconds: 2000), () {
                        _SearchValue=value;
                        print(value);
                        InterGetSearchList(value);
                      });
                    },
                  ),
                ),
                ElevatedButton(
                  onPressed: (){
                    print(this._SearchValue);
                  },
                  child: Text("搜索"),
                  style: ButtonStyle(
                    fixedSize: MaterialStateProperty.all(Size(90, 40))
                  ),
                )
              ],
            ),
            Column(
              children:_SearchItmeWidgetList
            )
          ],
        ),
      )
    );
  }
}


class SearchItme extends StatelessWidget {

  final String SearchValue;

  Function(String) SetSearch;

  SearchItme({Key? key ,required this.SearchValue,required this.SetSearch}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        GestureDetector(
          onTap: (){
            SetSearch(SearchValue);
          },
          child: Container(
            padding: EdgeInsets.all(10),
            alignment: Alignment.center,
            color: Colors.black26,
            height: 60,
            width: 360,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text("$SearchValue",style: TextStyle(
                    fontSize: 28
                ),),
                Icon(Icons.keyboard_arrow_right,size: 40,)
              ],
            ),
          ),
        )
      ],
    );
  }
}





